﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Xml.Linq;
using Microsoft.Practices.Unity;

namespace BaseTypesLib.Model
{

    public class MainScreen:Screen
    {

        private Field banner, title, status;

        public MainScreen()
        {
            VerticalScroll = true;
            VerticalScrollBar = true;
            UseAllHeight = true;
            UseAllWidth = true;
        }
        [Browsable(false)]
        public Field Status
        {
            get { return status; }
            set 
            {
                if (status != value)
                {
                    status = value;
                    OnPropertyChanged("Status");
                }
            }
        }
        [Browsable(false)]
        public Field Title
        {
            get { return title; }
            set
            {
                if (title != value)
                {
                    title = value;
                    OnPropertyChanged("Title");
                }
            }
        }
        [Browsable(false)]
        public Field Banner
        {
            get { return banner; }
            set
            {
                if (banner != value)
                {
                    banner = value;
                    OnPropertyChanged("Banner");
                }
            }
        }
        protected override XElement GetXmlElement()
        {
            XElement el = base.GetXmlElement();
            //remove use all width and use all height. for main screens these
            //are always true
            el.SetAttributeValue("UseAllWidth", null);
            el.SetAttributeValue("UseAllHeight", null);
            if (Status != null)
            {
                XElement s = new XElement("Status");
                s.AddFirst(Status.GetXml());
                el.Add(s);
            }
            if (Title != null)
            {
                XElement t = new XElement("Title");
                t.AddFirst(Title.GetXml());
                el.Add(t);
            }
            if (Banner != null)
            {
                XElement b = new XElement("Banner");
                b.AddFirst(Banner.GetXml());
                el.Add(b);
            }

            return el;
        }
        protected override void FromXmlElement(XElement elem)
        {

            var title = elem.Elements("Title").FirstOrDefault();
            if (title != null)
            {
                var tc = title.Elements().SingleOrDefault();
                Field field = Field.FromXml(tc);
                Title = field;
                title.Remove();
            }

            var banner = elem.Elements("Banner").FirstOrDefault();
            if (banner != null)
            {
                var b = banner.Elements().SingleOrDefault();
                Field field = Field.FromXml(b);
                Banner = field;
                banner.Remove();
            }

            var status = elem.Elements("Status").FirstOrDefault();
            if (status != null)
            {
                var s = status.Elements().SingleOrDefault();
                Field field = Field.FromXml(s);
                Status = field;
                status.Remove();
            }

            base.FromXmlElement(elem);
        }
    }
}
